/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tela;

//import com.lowagie.text.Image;
//import bd.Consultas;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.*;
import java.sql.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import bd.Consultas;
import java.util.*;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import relatorio.ReportUtils;
import tabela.Cultivares;


//import modelo.Imagens;

/**
 *
 * @author Edu
 */
public class CadastraBuscaCultivares extends javax.swing.JPanel {
    
    private int idCultivarSelecionado = 999999;
    private int ERRO = 0;
    private int SUCESSO = 1;
    private int INFO = 2;

    
    public CadastraBuscaCultivares() {
        initComponents();
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTextFieldCultivar = new javax.swing.JTextField();
        jTextFieldPorte = new javax.swing.JTextField();
        jTextFieldCiclo = new javax.swing.JTextField();
        jTextFieldMaturacao = new javax.swing.JTextField();
        jTextFieldHabtCresc = new javax.swing.JTextField();
        jTextFieldPesoSementes = new javax.swing.JTextField();
        jTextFieldCorFlor = new javax.swing.JTextField();
        jTextFieldCorPub = new javax.swing.JTextField();
        jTextFieldCorHilo = new javax.swing.JTextField();
        salvaCultivar = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jComboBoxEspecie = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jTextFieldObs = new javax.swing.JTextField();
        excluirCultivar = new javax.swing.JButton();
        novoCultivar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldCodCultivar = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        img = new javax.swing.JLabel();
        abrirFoto = new javax.swing.JButton();
        excluirFoto = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jTextFieldBuscaCultivar = new javax.swing.JTextField();
        jButtonBuscaCultivar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListCultivar = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        mensagens = new javax.swing.JTextArea();
        jLabel15 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(153, 153, 153), 2, true), "Dados do Cultivar", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 3, 12), new java.awt.Color(102, 102, 102))); // NOI18N

        jLabel2.setText("Cultivar:");

        jLabel3.setText("Porte:");

        jLabel4.setText("Ciclo:");

        jLabel5.setText("Grupo de Maturação:");

        jLabel6.setText("Hábito de Crescimento:");

        jLabel7.setText("Peso de Mil Sementes:");

        jLabel8.setText("Cor da Flor:");

        jLabel9.setText("Cor da Pubescência:");

        jLabel10.setText("Cor dol Hilo:");

        salvaCultivar.setText("Salvar");
        salvaCultivar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvaCultivarActionPerformed(evt);
            }
        });

        jLabel12.setText("Selecione a Espécie:");

        jComboBoxEspecie.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Milho", "Soja", "Feijao", "Trigo", "Triticale", "Aveia", "Azevem", "Canola", "Cevada", "Fumo", "Arroz" }));

        jLabel11.setText("Observações:");

        jTextFieldObs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldObsActionPerformed(evt);
            }
        });

        excluirCultivar.setText("Excluir");
        excluirCultivar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirCultivarActionPerformed(evt);
            }
        });

        novoCultivar.setText("Novo Cultivar");
        novoCultivar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novoCultivarActionPerformed(evt);
            }
        });

        jLabel1.setText("Codigo:");

        jTextFieldCodCultivar.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(novoCultivar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(excluirCultivar, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(salvaCultivar, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGap(66, 66, 66)
                                            .addComponent(jLabel8))
                                        .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(24, 24, 24)
                                        .addComponent(jLabel9)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextFieldCorPub, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                                    .addComponent(jTextFieldCorHilo)
                                    .addComponent(jTextFieldCorFlor)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(6, 6, 6)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldPesoSementes))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jTextFieldCultivar, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                    .addComponent(jTextFieldCiclo)
                                                    .addComponent(jTextFieldHabtCresc)
                                                    .addComponent(jTextFieldPorte)
                                                    .addComponent(jTextFieldMaturacao, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(0, 1, Short.MAX_VALUE))
                                            .addComponent(jComboBoxEspecie, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(46, 46, 46)
                                        .addComponent(jLabel11)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldObs))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldCodCultivar, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCodCultivar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxEspecie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCultivar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldPorte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldCiclo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldMaturacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldHabtCresc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldPesoSementes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextFieldCorFlor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextFieldCorPub, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTextFieldCorHilo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldObs, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(6, 6, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salvaCultivar)
                    .addComponent(excluirCultivar)
                    .addComponent(novoCultivar))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(153, 153, 153), 2, true), "Foto", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 3, 12), new java.awt.Color(102, 102, 102))); // NOI18N

        abrirFoto.setText("Abrir");
        abrirFoto.setEnabled(false);
        abrirFoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirFotoActionPerformed(evt);
            }
        });

        excluirFoto.setText("Excluir");
        excluirFoto.setEnabled(false);
        excluirFoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirFotoActionPerformed(evt);
            }
        });

        jButton1.setText("Imprimir");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(img, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(abrirFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(excluirFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(img, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(excluirFoto)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(abrirFoto)
                        .addComponent(jButton1)))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(153, 153, 153), 2, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 3, 14), java.awt.Color.gray)); // NOI18N

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel13.setText("Cultivar");

        jTextFieldBuscaCultivar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jButtonBuscaCultivar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButtonBuscaCultivar.setText("Buscar");
        jButtonBuscaCultivar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscaCultivarActionPerformed(evt);
            }
        });

        jListCultivar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jListCultivar.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListCultivarValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jListCultivar);

        mensagens.setBackground(new java.awt.Color(204, 204, 204));
        mensagens.setColumns(20);
        mensagens.setEditable(false);
        mensagens.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        mensagens.setForeground(new java.awt.Color(204, 0, 0));
        mensagens.setRows(5);
        mensagens.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jScrollPane3.setViewportView(mensagens);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane3)
                        .addComponent(jScrollPane2)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                            .addComponent(jTextFieldBuscaCultivar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButtonBuscaCultivar)))
                    .addComponent(jLabel13))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldBuscaCultivar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscaCultivar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel15.setIcon(new javax.swing.ImageIcon(getClass().getResource("/figura/seta.png"))); // NOI18N
        jLabel15.setMinimumSize(new java.awt.Dimension(10, 256));

        jLabel14.setBackground(new java.awt.Color(255, 255, 255));
        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel14.setText("Cultivar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void abrirFotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirFotoActionPerformed
        JFileChooser fileChooser = new JFileChooser();  
        fileChooser.setDialogTitle("Importar imagem");  
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG  GIF & PNG Images", "jpg", "gif","png");  //Cria um filtro  
        fileChooser.setFileFilter(filter);  //Altera o filtro do JFileChooser

        if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)  
        {    
            img.setIcon(new ImageIcon(fileChooser.getSelectedFile().getPath()));       
        }
        ima = fileChooser.getSelectedFile().getPath();
        System.out.print(ima);

    }//GEN-LAST:event_abrirFotoActionPerformed

    private void jTextFieldObsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldObsActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldObsActionPerformed

    private void excluirCultivarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirCultivarActionPerformed
        img.setIcon(null);
        
        String sql = "from Cultivares where cultivar='" + jTextFieldBuscaCultivar.getText()+"'";
        Cultivares cultivares = (Cultivares) Consultas.getUmaLinha(sql);
        if(cultivares != null) {
                if (Consultas.removeDoBanco(cultivares)) {
                    limpaComponentes();
                    //jButtonBuscaCultivar(null);
                    mensagem("Cultivar excluído com sucesso!", SUCESSO);
                } else {
                    mensagem("Erro ao excluir cultivar!", ERRO);
                }
        } else {
                mensagem("Erro ao excluir cultivar!", ERRO);
            }
    }//GEN-LAST:event_excluirCultivarActionPerformed

    private void salvaCultivarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvaCultivarActionPerformed
        if(verificaCamposEmBranco()) { 
            mensagem("Campos inválidos!", ERRO);
            return;
        }
        int idCultivar = idCultivarSelecionado;
         if (idCultivar == 999999) {
        if(verificaSeCultivarJaExiste(jTextFieldCultivar.getText())) {            
            mensagem("Cultivar já existente", ERRO);
            return;
        }

            idCultivar = getIdDisponivel();
        
        } 
         //byte[] bIma = new byte[(int) ima.length()];
         //try{
         //       FileInputStream fileInputStream = new FileInputStream(ima);
         //       fileInputStream.read(bIma);
         //       fileInputStream.close();
      //  } catch (Exception e)
       // {
        //    e.printStackTrace();
       // }
         
       Cultivares culti = new Cultivares(idCultivar);
       culti.setCultivar(jTextFieldCultivar.getText());
       culti.setCiclo((jTextFieldCiclo.getText()));
       culti.setCorflor(jTextFieldCorFlor.getText());
       culti.setCorhilo(jTextFieldCorHilo.getText());
       culti.setCresc(jTextFieldHabtCresc.getText());
       culti.setManut(jTextFieldMaturacao.getText());
       culti.setObs(jTextFieldObs.getText());
       culti.setPeso(jTextFieldPesoSementes.getText());
       culti.setPorte(jTextFieldPorte.getText());
       culti.setPub(jTextFieldCorPub.getText());
       culti.setEspecie(jComboBoxEspecie.getSelectedItem().toString());
      // culti.setImagemCultivar();
       
          if (Consultas.salvaNoBanco(culti)) {
            idCultivarSelecionado = idCultivar;       
            String mensagem = "Cultivar salvo com sucesso!";
            mensagem(mensagem, SUCESSO);
            String sql = "from Cultivares where upper(cultivar) like upper('%" + jTextFieldBuscaCultivar.getText() + "%')";
            preencheListaBusca(Consultas.getListObjetos(sql));
            //JOptionPane.showMessageDialog(null, "Cliente salvo com sucesso", "Salvar", JOptionPane.INFORMATION_MESSAGE);
        } else {
            String mensagem = "Erro ao salvar cultivar!";
            mensagem(mensagem, ERRO);
            //JOptionPane.showMessageDialog(null, "Erro ao salvar cliente", "Erro", JOptionPane.ERROR);
        }
    }//GEN-LAST:event_salvaCultivarActionPerformed

    private void jButtonBuscaCultivarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscaCultivarActionPerformed
        String sql = "from Cultivares where upper(cultivar) like upper('%" + jTextFieldBuscaCultivar.getText() + "%')";
        preencheListaBusca(Consultas.getListObjetos(sql));
    }//GEN-LAST:event_jButtonBuscaCultivarActionPerformed

    private void jListCultivarValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListCultivarValueChanged
      if (jListCultivar.getSelectedValue() == null) {
            return;
        }
        limpaComponentes();
        System.out.println("Selecionado=" + jListCultivar.getSelectedValue());
        String sql = "from Cultivares where cultivar='" + jListCultivar.getSelectedValue().toString()+"'";
        List resultList = Consultas.getListObjetos(sql);
        for (Object o : resultList) {
            
            Cultivares culti = (Cultivares) o;
            
            
            idCultivarSelecionado = culti.getIdcultivar();
            jTextFieldCiclo.setText(culti.getCiclo());
            jTextFieldCorFlor.setText(culti.getCorflor());
            jTextFieldCorHilo.setText(culti.getCorhilo());
            jTextFieldCorPub.setText(culti.getPub());
            jTextFieldCultivar.setText(culti.getCultivar());
            jTextFieldHabtCresc.setText(culti.getCresc());  
            jTextFieldMaturacao.setText(culti.getManut()); 
            jTextFieldObs.setText(culti.getObs());
            jTextFieldPorte.setText(culti.getPorte());
            jTextFieldPesoSementes.setText(culti.getPeso());
            jTextFieldCodCultivar.setText(Integer.toString(culti.getIdcultivar()));
            jComboBoxEspecie.setSelectedItem(culti.getEspecie());
            ImageIcon imagem;
            byte[] imagemBytes;
            try {
                FileOutputStream out = new FileOutputStream("C:/foto/out.jpg");
             //   out.write(culti.getImagemCultivar());
                //imagemBytes = culti.getImagemCultivar();
                
                //imagem = new ImageIcon(imagemBytes);
                
                //img.setIcon(imagem);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }  
    }//GEN-LAST:event_jListCultivarValueChanged

    private void excluirFotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirFotoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_excluirFotoActionPerformed

    private void novoCultivarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novoCultivarActionPerformed
        idCultivarSelecionado = 999999;
        limpaComponentes();
        jListCultivar.clearSelection();
        mensagem("Preencha os dados", INFO); 
    }//GEN-LAST:event_novoCultivarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
/*   
        String arquivo = "C:/Users/Eduardo/Documents/NetBeansProjects/Softlabs/src/relatorio/teste.jasper";
        Map<String, Object> parametros = new HashMap<String, Object>();
        Cultivares culti = new Cultivares();
        // criando os dados que serão passados ao datasource
        List<Cultivares> dados = new ArrayList<Cultivares>();

        culti.setCultivar(jTextFieldCultivar.getText());
       culti.setCiclo((jTextFieldCiclo.getText()));
       culti.setCorflor(jTextFieldCorFlor.getText());
       culti.setCorhilo(jTextFieldCorHilo.getText());
       culti.setCresc(jTextFieldHabtCresc.getText());
       culti.setManut(jTextFieldMaturacao.getText());
       culti.setObs(jTextFieldObs.getText());
       culti.setPeso(jTextFieldPesoSementes.getText());
       culti.setPorte(jTextFieldPorte.getText());
       culti.setPub(jTextFieldCorPub.getText());
       culti.setEspecie(jComboBoxEspecie.getSelectedItem().toString());
        //for ( long i = 1; i <= 50; i++ ) {
            
          //  c.setManut("Nome Cliente " + i);
           dados.add( culti );
        //}

        // criando o datasource com os dados criados
        JRDataSource ds = new JRBeanCollectionDataSource( dados );

        try {
            JasperReport inputStream = (JasperReport) JRLoader.loadObject(arquivo);
            // passando o datasource para o método de criação e exibição do relatório
            ReportUtils.openReportDs( "Cultivares", inputStream, parametros, ds );

        } catch ( JRException exc ) {
            exc.printStackTrace();
        }*/
    }//GEN-LAST:event_jButton1ActionPerformed
public void limpaComponentes() {
        jTextFieldCiclo.setText("");
        jTextFieldCorFlor.setText("");
        jTextFieldCorHilo.setText("");
        jTextFieldCorPub.setText("");
        jTextFieldCultivar.setText("");
        jTextFieldHabtCresc.setText("");
        jTextFieldHabtCresc.setText("");    
        jTextFieldMaturacao.setText(""); 
        jTextFieldObs.setText("");
        jTextFieldPorte.setText("");
        jTextFieldPesoSementes.setText("");
        jComboBoxEspecie.setSelectedIndex(WIDTH-1);
   }
private boolean verificaCamposEmBranco(){
       if(jTextFieldCiclo.getText().equals(""))
           return true;
       if(jTextFieldCorFlor.getText().equals(""))
           return true;
       if(jTextFieldCorHilo.getText().equals(""))
           return true;
       if(jTextFieldCorPub.getText().equals(""))
           return true;
       if(jTextFieldCultivar.getText().equals(""))
           return true;
       if(jTextFieldHabtCresc.getText().equals(""))
           return true;
       if(jTextFieldMaturacao.getText().equals(""))
           return true;
       if(jTextFieldObs.getText().equals(""))
           return true;
       if(jTextFieldPorte.getText().equals(""))
           return true;
       if(jTextFieldPesoSementes.getText().equals(""))
           return true;
       return false;
   }

     private void mensagem(String msg, int tipo) {
        if(tipo == 0) { //erro
            mensagens.setForeground(Color.RED);           
            mensagens.setText(msg);
        } else if(tipo == 1) { //sucesso
            mensagens.setForeground(Color.blue);           
            mensagens.setText(msg);
        } if(tipo == 2) { //info
            mensagens.setForeground(Color.black);           
            mensagens.setText(msg);
        }
     }

     private void preencheListaBusca(List resultList) {
        Vector tableData = new Vector();
        for (Object o : resultList) {
            Cultivares cultivar = (Cultivares) o;
            String cultiva = cultivar.getCultivar();         
            tableData.add(cultiva);
            System.out.println(cultiva);
        }
        if (tableData.size() == 0) {
            tableData.add("Nenhuma ocorrência");
        }
        jListCultivar.setListData(tableData);
    }
     
     private boolean verificaSeCultivarJaExiste(String cultivar) {
        boolean achou = false;
        int quantidade = Consultas.getCount("select count(*) from Cultivares where cultivar='"+cultivar+"'");
        if(quantidade > 0) {
            return true;
        }
        return false;
     }
     
     private int getIdDisponivel() {
        boolean achou = false;
        int quantidade = Consultas.getCount("select count(*) from Cultivares");
        int novoID = quantidade + 1;
        while (!achou) {
            int existeEsseId = Consultas.getCount("select count(cultivar) from Cultivares where idcultivar =" + novoID);
            if (existeEsseId == 0) {
                return novoID;
            }
            novoID += 1;
        }
        return 0;
     }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton abrirFoto;
    private javax.swing.JButton excluirCultivar;
    private javax.swing.JButton excluirFoto;
    private javax.swing.JLabel img;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonBuscaCultivar;
    private javax.swing.JComboBox jComboBoxEspecie;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jListCultivar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTextFieldBuscaCultivar;
    private javax.swing.JTextField jTextFieldCiclo;
    private javax.swing.JTextField jTextFieldCodCultivar;
    private javax.swing.JTextField jTextFieldCorFlor;
    private javax.swing.JTextField jTextFieldCorHilo;
    private javax.swing.JTextField jTextFieldCorPub;
    private javax.swing.JTextField jTextFieldCultivar;
    private javax.swing.JTextField jTextFieldHabtCresc;
    private javax.swing.JTextField jTextFieldMaturacao;
    private javax.swing.JTextField jTextFieldObs;
    private javax.swing.JTextField jTextFieldPesoSementes;
    private javax.swing.JTextField jTextFieldPorte;
    private javax.swing.JTextArea mensagens;
    private javax.swing.JButton novoCultivar;
    private javax.swing.JButton salvaCultivar;
    // End of variables declaration//GEN-END:variables
     String ima = null;
     byte[] foto = null;
     BufferedImage imagem;
     byte[] byteArray;
}
